<?
if ( !defined( "_MYDB_CLASS" ) ){
	define("_MYDB_CLASS", 1 );

	//Cac ham lien quan den ket noi, dong database - NDM note 17/10/2003
	class my_db	{
		var $connect_id;
	//	var $dbstyle;
		var $stmt_id = array();

		//function to open database
		function open($hostname, $user, $password, $database, $dbstyle)
		{
			$this->dbstyle = $dbstyle;
			switch ($this->dbstyle) {
				case 1:
					$this->connect_id = mysql_connect($hostname,$user,$password);
					break;
				case 2:
					$this->connect_id = mssql_connect($hostname,$user,$password);
					break;
			}
			if( $this->connect_id )
			{
				switch ($this->dbstyle) {
					case 1:
						if(mysql_select_db($database,$this->connect_id))
							return $this->connect_id;
						break;
					case 2:
						if(mssql_select_db($database,$this->connect_id))
							return $this->connect_id;
						break;
				}
			}
			else
			{
				return 0;
			}
		}

		//Closes the database connection and frees any query results left.
		function close()
		{
			switch ($this->dbstyle) {
				case 1:
					$result = mysql_close($this->connect_id);
					break;
				case 2:
					$result = mssql_close($this->connect_id);
					break;
			}
			return	$result;
		}

		//Execute SQL string.
		function execute($strSql)
		{
			switch ($this->dbstyle) {
				case 1:
					$result = mysql_query($strSql,$this->connect_id);
					break;
				case 2:
					$result = mssql_query($strSql,$this->connect_id);
					break;
			}
			return	$result;
		}

		//Function used by the constructor of query.
		function addstmt($stmt_id)
		{
			$this->stmt_id[]=$stmt_id;
		}

		//Closes the database connection and frees any query results left.
		function stmtclose()
		{
			if($this->stmt_id && is_array($this->stmt_id))
			{
				while (list($key,$val)=each($this->stmt_id))
				{
					switch ($this->dbstyle)
					{
						case 1:
							if($val) mysql_free_result($val);
							break;
						case 2:
							if($val) mssql_free_result($val);
							break;
					}
				}
			}
		 }

		//reurn number of rows after query(insert,update,delete)
		function affectedrows()
		{
			switch ($this->dbstyle) {
				case 1:
					return mysql_affected_rows($this->connect_id) ;
					break;
				case 2:
					break;
			}
		}

		function last_id()
		{
			switch ($this->dbstyle) {
				case 1:
					return mysql_insert_id($this->connect_id) ;
					break;
				case 2:
					break;
			}
		}

	} /* End of Defination of my_db Class */


/*	Defination of db_query Class
Content : implement a query
Cac ham lien quan den truy van database  - NDM note 17/10/2003
*/
	class db_query
	{
		var $row;
		var $stmt;
		var $dbstyle;

		//new a query and directly execute the normal query
		function db_query(&$db, $query="")
		{
			//echo "$query<br><br>";
			$this->dbstyle = $db->dbstyle;
			if($query!="" && $db->connect_id)
			{
				switch ($this->dbstyle) {
					case 1:
						//echo "db->connect_id: $db->connect_id<br>";
						$this->stmt  = mysql_query($query,$db->connect_id);
						
						break;
					case 2:
						//echo "$query<br>";
						$this->stmt  = mssql_query($query,$db->connect_id);
						break;
				}
				if ($this->stmt){
					return  0;
				}
				$db->addstmt($this->stmt);
				return $this->stmt;
			}else{
				return 0;
			}
		}

		// fetch the next row of stmt
		function getrow()
		{
			if($this->stmt)
			{
				switch ($this->dbstyle) {
					case 1:
						$this->row = mysql_fetch_array($this->stmt);
						break;
					case 2:
						$this->row = mssql_fetch_array($this->stmt);
						break;
				}
			}
			return  $this->row;
		}

		function numcols()
		{
			switch ($this->dbstyle) {
				case 1:
					$ncols = mysql_num_fields($this->stmt);
					break;
				case 2:
					$ncols = mssql_num_fields($this->stmt);
					break;
			}
			return $ncols;
		}

		function getcolumnname($i)
		{
			switch ($this->dbstyle) {
				case 1:
					$column_name = mysql_field_name($this->stmt,$i);
					break;
				case 2:
					$column_name = mssql_field_name($this->stmt,$i);
					break;
			}
			return $column_name;
		}

		// get the number of rows of $stmt
		function numrows()
		{
			switch ($this->dbstyle)
			{
				case 1:
					$number = mysql_num_rows($this->stmt) ;
					break;
				case 2:
					$number = mssql_num_rows($this->stmt) ;
					break;
			}
			return $number;
		}

	} /* End of Definition of my_query Class */

} /* End of define */

$dbconn = new my_db;
if(!$dbconn->open(DATABASE_HOST,DATABASE_USER,DATABASE_PASSWORD,DATABASE_NAME,DBSTYLE)){
	$message = "Can not connect to database. Please contact with DBA.";
	print($message);
	exit();
}
//khai bao DB MSSQL
//$db_ado = new COM("ADODB.Connection");
//$dsn = "DRIVER={SQL Server}; SERVER=".DATABASE_HOST.";UID=".DATABASE_USER.";PWD=".DATABASE_PASSWORD."; DATABASE=".DATABASE_NAME."";
//$db_ado->Open($dsn);
//test su dung
/*
$rs = $db_ado->Execute("select * from congviec A, duan B where A.congviec_duan = B.duan_id and congviec_duan = 7 and congviec_parent = 0 order by B.duan_order, A.congviec_order ");
while (!$rs->EOF)
{
	$giatri = $rs->Fields['congviec_name']->Value;
	$giatri = htmlspecialchars($giatri);
   echo "$giatri<BR>";
   $rs->MoveNext();
}
//*/
?>